package com.vitalfit.controller;

import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.dto.FoodItemDTO;
import com.vitalfit.model.entity.food.FoodItem;
import com.vitalfit.model.vo.FoodItemPageVO;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.service.serviceImpl.FoodItemServiceImpl;
import com.vitalfit.utils.ResultVOUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/food-items")
@RequiredArgsConstructor
@Validated
public class FoodItemController {

    private final FoodItemServiceImpl foodServiceImpl;

    // 添加食品
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping
    public ResultVO<FoodItem> addFoodItem(@RequestBody @Valid FoodItemDTO foodItemDTO) {
        return ResultVOUtil.buildResult(ResultEnum.POST_SUCCESS,
                foodServiceImpl.addFoodItem(foodItemDTO));
    }

    // 更新食品信息
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PutMapping("/{foodItemId}")
    public ResultVO<FoodItem> updateFooItem(@PathVariable Integer foodItemId, @RequestBody @Valid FoodItemDTO foodItemDTO) {
        return ResultVOUtil.buildResult(ResultEnum.PUT_SUCCESS,foodServiceImpl.updateFoodItem(foodItemId, foodItemDTO));
    }

    // 获取食品列表
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER') or hasRole('ROLE_CUSTOMER')")
    @GetMapping
    public ResultVO<FoodItemPageVO> listAllFoodItems(@RequestParam(defaultValue = "0") int page,
                                                     @RequestParam(defaultValue = "10") int size) {
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,foodServiceImpl.listAllFoodItems(page,size));
    }

    // 获取特定ID的食品
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER') or hasRole('ROLE_CUSTOMER')")
    @GetMapping("/{foodItemId}")
    public ResultVO<FoodItem> getFoodItemById(@PathVariable Integer foodItemId) {
        FoodItem foodItemVO = foodServiceImpl.getFoodById(foodItemId);
        return ResultVOUtil.buildResult(ResultEnum.GET_SUCCESS,foodItemVO);
    }
}
